<template>
  <div class="mod-config">
    <el-form size="mini" :inline="true" :model="dataForm" @keyup.enter.native="getDataList(true)">
      <el-form-item>
        <el-input clearable v-model="dataForm.createUser" placeholder="创建人"></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dataForm.displayTime"
          type="daterange"
          align="left"
          style="width: 380px;"
          unlink-panels
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="dateChange"
          :picker-options="createTimePickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(true)" type="primary" plain icon="el-icon-search">查询</el-button>
        <el-button v-if="isAuth('sys:dept:save')" type="primary" @click="addOrUpdateHandle()" icon="el-icon-edit">新增</el-button>
      </el-form-item>
      <el-form-item style="font-size: 17px; font-weight: bold;">
        机构数量：{{totalCount}}
      </el-form-item>
    </el-form>
    <el-table
      highlight-current-row
      :height="tableHeight"
      :data="dataList"
      row-key="deptId"
      border
      :header-cell-style="{background:'#F5F7FA'}"
      :default-sort = "{prop: 'orderNum', order: 'ascending'}"
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        sortable
        prop="deptCode"
        header-align="center"
        align="center"
        label="机构代码">
      </el-table-column>
      <el-table-column
        sortable
        prop="deptName"
        header-align="center"
        align="center"
        label="机构名称">
      </el-table-column>
      <el-table-column
        sortable
        prop="parentDeptName"
        header-align="center"
        align="center"
        label="上级机构">
      </el-table-column>
      <el-table-column
        sortable
        prop="deptTypeName"
        header-align="center"
        align="center"
        label="机构类型">
      </el-table-column>
      <el-table-column
        sortable
        prop="orderNum"
        header-align="center"
        align="center"
        label="排序">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" style="padding: 0;" v-if="isAuth('sys:dept:update')" @click="addOrUpdateHandle(scope.row.deptId)">修改</el-button>
          <el-button type="text" style="padding: 0;" v-if="isAuth('sys:dept:delete')" @click="deleteHandle(scope.row.deptId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 弹窗, 查看 -->
    <detail v-if="detailVisible" ref="detail"></detail>
  </div>
</template>

<script>
  import addOrUpdate from './dept-add-or-update'
  import { dateSelectShorts } from '@/utils/common'
  import { treeDataTranslate } from '@/utils'

  export default {
    data () {
      return {
        dataForm: {
          order: 'orderNum asc',
          createUser: '',
          displayTime: '',
          createTime: '',
          createTimeEnd: ''
        },
        tableHeight: this.$store.state.common.documentClientHeight - 260,
        dataList: [],
        totalCount: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        detailVisible: false,
        createTimePickerOptions: {
          shortcuts: dateSelectShorts()
        }
      }
    },
    components: {
      addOrUpdate
    },
    activated () {
      this.getDataList(true)
    },
    methods: {
      // 获取数据列表
      getDataList (isReload) {
        if (isReload) {
          this.pageNo = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.requestUrl('/sys/dept/list'),
          method: 'get',
          params: this.$http.requestParams({
            'order': this.dataForm.order,
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'createUser': this.dataForm.createUser,
            'createTime': this.dataForm.createTime,
            'createTimeEnd': this.dataForm.createTimeEnd
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = treeDataTranslate(data.page.list, 'deptId', 'parentDeptId')
            // console.log(JSON.stringify(this.dataList))
            this.totalCount = data.page.totalCount
          } else {
            this.dataList = []
            this.totalCount = 0
          }
          this.dataListLoading = false
        })
      },
      // 时间选择change事件
      dateChange (val) {
        this.dataForm.createTime = val == null ? '' : val[0]
        this.dataForm.createTimeEnd = val == null ? '' : val[1]
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm(`确定对id=${id}的记录进行删除?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.requestUrl('/sys/dept/delete'),
            method: 'post',
            data: id
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList(true)
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
